﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using BusinessObjects;
using BusinessObjects.Constants;
using DataEntities;
using DataProvider;
using SQLDataProvider;

namespace BusinessServices
{
    public class YuZuService
    {
        private readonly IBaseRepository<YuZuInfo> _yuZuRepository;

        public YuZuService()
        {
            _yuZuRepository = new BaseRepository<YuZuInfo>();
        }

        /// <summary>
        /// 获取所有预租信息
        /// </summary>
        public List<YuZuInfo> GetAll()
        {
            return _yuZuRepository.GetAll(p => true, JoinKeHuAndCar);
        }

        /// <summary>
        /// 获取被预租了的车辆Id
        /// </summary>
        public List<int> GetYuZuCarIds()
        {
            return _yuZuRepository.Get(o => o.iCarid, o => !o.bYZZF);
        }

        /// <summary>
        /// 获取今天待处理的预租信息条数
        /// </summary>
        public int GetPreconcertCount()
        {
            return _yuZuRepository.Count(p => p.tYZDate == DateTime.Now && !p.bYZZF);
        }

        /// <summary>
        /// 根据客户Id获取简要预租信息
        /// </summary>
        /// <param name="keHuId">客户Id</param>
        public SimpleInfoContract GetSimpleYuZuInfoByKeHuId(int keHuId)
        {
            return GetSimpleYuZuInfo(o => o.iKeHuId == keHuId);
        }

        /// <summary>
        /// 根据车辆Id获取车辆预租信息
        /// </summary>
        /// <param name="carId">车辆Id</param>
        public SimpleInfoContract GetSimpleYuZuInfoByCarId(int carId)
        {
            return GetSimpleYuZuInfo(o => o.iCarid == carId);
        }

        #region 获取并构建预租信息
        private SimpleInfoContract GetSimpleYuZuInfo(Func<YuZuInfo, bool> condition)
        {
            var chuZuInfo = _yuZuRepository.GetAll(condition, JoinCarInfo);
            var result = new SimpleInfoContract();
            var strBuilder = new StringBuilder();

            chuZuInfo.ForEach(yzInfo => GenerateStr(yzInfo, ref strBuilder));
            result.Count = chuZuInfo.Count;
            result.Content = strBuilder.Length == 0 ? Constants.NoYuZuInfo : strBuilder.ToString();
            return result;
        }

        private DataLoadOptions JoinCarInfo()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<YuZuInfo>(o => o.CarInfo);
            return loadOption;
        }

        private void GenerateStr(YuZuInfo yz, ref StringBuilder strBuilder)
        {
            strBuilder.AppendFormat(Constants.SimpleYuZuInfo, yz.CarInfo.sCType, yz.dYuFuJE, yz.tYZDate);
        }
        #endregion

        /// <summary>
        /// 添加一条预租信息
        /// </summary>
        /// <param name="yuZuInfo"></param>
        /// <returns></returns>
        public YuZuInfo Add(YuZuInfo yuZuInfo)
        {
            return _yuZuRepository.Add(yuZuInfo);
        }

        /// <summary>
        /// 更新预租信息
        /// </summary>
        /// <param name="yuZuId">预租单Id</param>
        /// <param name="update">包含更新操作的委托，在此委托中进行更改操作</param>
        public YuZuInfo Update(int yuZuId, Action<YuZuInfo> update)
        {
            return _yuZuRepository.Update(p => p.iYZDId == yuZuId, update);
        }

        /// <summary>
        /// 删除预租单 
        /// </summary>
        /// <param name="yuZuId">预租单Id</param>
        public bool Delete(int yuZuId)
        {
            return _yuZuRepository.Delete(p => p.iYZDId == yuZuId);
        }

        private DataLoadOptions JoinKeHuAndCar()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<YuZuInfo>(p => p.KeHuInfo);
            loadOption.LoadWith<YuZuInfo>(p => p.CarInfo);
            return loadOption;
        }
    }
}
